﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DCTLib.Common;
using DCTAccess.Business.Administrator;

public partial class AdminZone_AdminDetail : System.Web.UI.Page
{
    protected int id;
    public string MessagePS
    {
        get { return lblPassErr.Text; }
        set { lblPassErr.Text = value; }
    }
    public string MessageIS
    {
        get { return lblInfoErr.Text; }
        set { lblInfoErr.Text = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BinData();
        }
    }

    private void BinData()
    {
        id = AppRequestInfo.ID;
        if (id != -1)
        {
            Administrator admin = AdministratorController.GetAdministratorByID(id);
            if (admin != null)
            {
                txtFullName.Text = admin.FullName;
                txtUsername.Text = admin.UserName;
                txtBirthday.Text = admin.Birthday;
                txtRegDate.Text = admin.RegDate;
                txtMobile.Text = admin.Mobile;
                txtAddress.Text = admin.Address;
                txtEmail.Text = admin.Email;
            }
        }
    }

    private bool ValidateSaveChangeData()
    {
        bool validated = true;
        if (ValidationHelper.IsEmpty(txtFullName))
        {
            MessageIS = AppMessages.RequireField("Full Name") + "<br>";
            validated = false;
        }

        if (ValidationHelper.IsEmpty(txtEmail))
        {
            MessageIS = AppMessages.RequireField("Email") + "<br>";
            validated = false;
        }

        if (this.ddlRole.SelectedIndex == 0)
        {
            MessageIS = AppMessages.RequireField("Role") + "<br>";
            validated = false;
        }

        return validated;
    }

    public bool ValidateData()
    {
        bool validated = true;
        if (ValidationHelper.IsEmpty(txtOldPassword.Text.Trim()))
        {
            MessagePS += AppMessages.RequireField("Old Password") + "<br>";
            validated = false;
        }
        if (ValidationHelper.IsEmpty(txtNewPassword.Text.Trim()))
        {
            MessagePS += AppMessages.RequireField("New Password") + "<br>";
            validated = false;
        }
        if (ValidationHelper.IsEmpty(txtReNewPassword.Text.Trim()))
        {
            MessagePS += AppMessages.RequireField("Confirm New Password") + "<br>";
            validated = false;
        }
        else if (txtNewPassword.Text.Trim() != txtReNewPassword.Text.Trim())
        {
            MessagePS += AppMessages.MustMatch("New Password", "Confirm New Password") + "<br>";
            validated = false;
        }
        return validated;
    }

    protected Administrator GetAdminForChangePass()
    {
        Administrator ad = new Administrator();
        ad.ID = AppRequestInfo.ID;
        ad.HashedPassword = MD5Helper.GetMD5Hash(txtNewPassword.Text);
        return ad;
    }

    private Administrator GetAminForChange()
    {
        Administrator admin = new Administrator();
        admin.ID = AppRequestInfo.ID;
        admin.FullName = txtFullName.Text;
        admin.Address = txtAddress.Text;
        admin.Email = txtEmail.Text;
        admin.Role = int.Parse(this.ddlRole.SelectedValue);
        return admin;
    }

    protected void btnSavePass_Click(object sender, EventArgs e)
    {
        if (ValidateData())
        {
            Administrator adv = GetAdminForChangePass();
            Administrator admin = AdministratorController.GetAdministratorByID(adv.ID);

            if (MD5Helper.GetMD5Hash(txtOldPassword.Text) == admin.HashedPassword)
            {
                if (AdministratorController.ChangePassword(adv))
                {
                    MessagePS = AppMessages.UPDATE_SUCCESSFULLY;
                }
                else
                {
                    MessagePS = AppMessages.CANNOT_UPDATE;
                }
            }
            else
            {
                MessagePS = AppMessages.NOT_CORRECT_OLD_PASSWORD;
            }
        }
        else
        {
            MessagePS = AppMessages.NOT_CORRECT_OLD_PASSWORD;
        }
    }

    protected void btnSaveInfo_Click(object sender, EventArgs e)
    {
        if (ValidateSaveChangeData())
        {
            Administrator admin = GetAminForChange();
            MessageIS = admin.ID + "-" + txtFullName.Text + "-" + admin.Address + "-" + admin.Email + "-" + admin.Role;
            if (AdministratorController.Update(admin))
            {
                MessageIS = AppMessages.UPDATE_SUCCESSFULLY;
                BinData();
            }
            else
            {
                MessageIS = AppMessages.CANNOT_UPDATE;
            }
        }
    }
}
